package com.indusfo.spc.service.impl;

import com.indusfo.spc.exception.GlobalException;
import com.indusfo.spc.exception.ModifyFailedException;
import com.indusfo.spc.exception.ParamsErrorException;
import com.indusfo.spc.mapper.PlanOfflineMapper;
import com.indusfo.spc.pojo.PlanOffline;
import com.indusfo.spc.service.PlanOfflineService;
import com.indusfo.spc.vo.HintMessage;
import com.indusfo.spc.vo.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: 熊冰
 * @Date: 2019/11/11 10:38
 * 任务单维修流程
 */

@Service
public class PlanOfflineServiceImpl implements PlanOfflineService {
	// 声明日志的静态变量
	private static final Logger logger = LoggerFactory.getLogger(PlanOfflineServiceImpl.class);

	@Resource
	private PlanOfflineMapper planOfflineMapper;
	//查询
	@Override
	public JSONObject queryPlanOffline(PlanOffline planOffline) {
		try {
			//获取页面的数据个数
			Integer pagesize = planOffline.getPagesize();
			//获取查询的页数
			Integer pageindex = planOffline.getPageindex();
			if(pagesize!=null && pageindex !=null){
				planOffline.setIncept(pagesize*(pageindex-1));
			}
			//执行查询操作
			List<PlanOffline> planOfflineList=planOfflineMapper.queryPlanOffline(planOffline);

			//如果页数和页面数量都不为空的情况下进行计数
			if(planOfflineList.isEmpty()){
				return  JSONObject.oK(HintMessage.QUERY_NULL,planOfflineList,0);
			}else{
				Integer count= planOfflineMapper.countPlanOffline(planOffline);
				return JSONObject.oK(HintMessage.QUERY_SUCCESS,planOfflineList,count);
			}

		} catch (GlobalException e) {
			logger.error(e.getMessage(), e);
			return JSONObject.build(500, e.getMessage());

		}
	}
	//新增
	@Override
	public JSONObject insertPlanOffline(PlanOffline planOffline) {
		try{
			if(planOffline.getPlanOfflineId()!=null){
				throw new ModifyFailedException(HintMessage.INSERT_ID);
			}
			//初始化基础数据
			if(planOffline.getDataState()==null){
				planOffline.setDataState(1);
			}
			//判重
			checkParam(planOffline);
			//执行新增操作
			Integer row=planOfflineMapper.insertPlanOffline(planOffline);
			if(row==0){
				throw new ModifyFailedException(HintMessage.INSERT_DEFEATED);
			}
			return JSONObject.oK(HintMessage.INSERT_SUCCESS);
		}catch(GlobalException e) {
			logger.error(e.getMessage());
			return JSONObject.build(500, e.getMessage());
		}
	}
	//修改
	@Override
	public JSONObject updatePlanOffline(PlanOffline planOffline) {
		try {
			if(planOffline.getPlanOfflineId()==null) {
				throw new ParamsErrorException(HintMessage.UPDATE_ID);
			}
			//初始化基础数据
			if(planOffline.getDataState()==null){
				planOffline.setDataState(1);
			}
			//判重
			checkParam(planOffline);
			//执行修改操作
			int row = planOfflineMapper.updatePlanOffline(planOffline);
			if(row == 0) {
				throw new ModifyFailedException(HintMessage.UPDATE_DEFEATED);
			}
			return JSONObject.oK(HintMessage.UPDATE_SUCCESS);
		}catch (GlobalException e) {//捕获自定义异常
			logger.error(e.getMessage());
			return JSONObject.build(500, e.getMessage());
		}
	}
	//删除
	@Override
	public JSONObject deletePlanOffline(List<Integer> planOfflineIds, Integer dataState) {
		try{
			//空值判断
			if(dataState == null) {
				throw new ParamsErrorException(HintMessage.DATASTATE_NULL);
			}
			if(planOfflineIds == null) {
				throw new ParamsErrorException(HintMessage.LIST_NULL);
			}
			//记录异常信息
			String msg = "";
			switch (dataState) {
				case 1:
					msg = HintMessage.START_VALUE;break;
				case 2:
					msg = HintMessage.DELETE_VALUE;break;
				case 3:
					msg = HintMessage.STOP_VALUE;break;
				default:
					throw new ParamsErrorException(HintMessage.VIOLATION_OPERATION);
			}
			//执行删除操作
			int row = planOfflineMapper.deletePlanOffline(planOfflineIds,dataState);
			//声明msg字符串，用来存抛出的异常
			if (row == 0) {
				msg=msg+HintMessage.DEFEATED_VALUE;
			} else {
				msg=msg+HintMessage.SUCCESS_VALUE;
			}
			return JSONObject.oK(msg);
		}catch(GlobalException e) {
			logger.error(e.getMessage());
			return JSONObject.build(500, e.getMessage());
		}
	}

	//判重
	public void checkParam(PlanOffline planOffline){


		// 执行判重操作
		Integer Counts = planOfflineMapper.checkPlanOffline(planOffline);
		if (Counts !=0) {
			throw new ParamsErrorException(HintMessage.REPEATING_DATA);
		}

	}


}